{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import pickle\n",
    "\n",
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn import metrics\n",
    "from sklearn.model_selection import cross_validate\n",
    "from sklearn.model_selection import cross_val_score\n",
    "from sklearn.tree import export_graphviz\n",
    "from sklearn.neighbors import KNeighborsRegressor\n",
    "from sklearn.neural_network import MLPRegressor\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "\n",
    "import warnings\n",
    "import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "filename = \"models_SVR/models.pkl\"\n",
    "stopfractionsname = \"stopfractions.pkl\"\n",
    "models = pickle.load(open(filename, \"rb\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_prediction(route,direction,datestring,stopA,stopB):\n",
    "    datetime_departure = datetime.datetime.strptime(datestring,'%Y-%m-%d %H:%M:%S')\n",
    "            \n",
    "    forecast = find_forecast(datetime_departure)        \n",
    "    \n",
    "    prediction = predict_travel_time(route,direction,datetime_departure,forecast['feels_like'],forecast['weather_main'],stopA,stopB)\n",
    "    \n",
    "    return int(prediction)\n",
    "\n",
    "def find_forecast(datetime_departure):\n",
    "    forecast = get_5day_forcast()\n",
    "    for i,f in enumerate(forecast['list']):\n",
    "        if datetime.datetime.fromtimestamp(f['dt']) > datetime_departure:\n",
    "            f_out = {'feels_like':f['main']['feels_like']-273.15,'weather_main':f['weather'][0]['main']}\n",
    "            return f_out\n",
    "    avg_monthly_weather = pickle.load(open('avg_monthly_weather.pkl','rb'))\n",
    "    f_out = {\n",
    "        'feels_like':avg_monthly_weather['feels_like'].iloc[datetime_departure.month],\n",
    "        'weather_main':avg_monthly_weather['weather_main'].iloc[datetime_departure.month]\n",
    "    }\n",
    "    return f_out\n",
    "    \n",
    "def predict_travel_time(route,direction,datetime_departure,feels_like,weather_main,stopA,stopB):\n",
    "    departure_times = pickle.load(open('departure_times.pkl','rb'))\n",
    "    \n",
    "    stopfractionsname = \"stopfractions.pkl\"\n",
    "    stopfractions = pickle.load(open(stopfractionsname,'rb'))\n",
    "\n",
    "    total_triptime = predict_total_triptime(route,direction,datetime_departure,feels_like,weather_main)\n",
    "    \n",
    "    stopA_frac = stopfractions[stopfractions['LINEID']==route][stopfractions['DIRECTION']==direction][stopfractions['STOPPOINTID']==stopA]['TRIP_FRAC'].iloc[0]\n",
    "    stopB_frac = stopfractions[stopfractions['LINEID']==route][stopfractions['DIRECTION']==direction][stopfractions['STOPPOINTID']==stopB]['TRIP_FRAC'].iloc[0]\n",
    "    fraction = stopB_frac - stopA_frac\n",
    "    \n",
    "    schedule = [datetime_departure.replace(hour=0, minute=0, second=0, microsecond=0) + datetime.timedelta(seconds=int(t)) for t in departure_times[route][direction][datetime_departure.weekday()]]\n",
    "\n",
    "    predictions = predict_total_triptimes(route,direction,schedule,feels_like,weather_main)\n",
    "    for i, start_time in enumerate(schedule):\n",
    "        if start_time + datetime.timedelta(seconds=predictions[i]*stopA_frac) > datetime_departure:\n",
    "            prediction = predictions[i]*fraction\n",
    "            return prediction\n",
    "            \n",
    "def predict_fractional_triptime(route,direction,datetime_departure,feels_like,weather_main,stopA,stopB):\n",
    "    stopfractionsname = \"stopfractions.pkl\"\n",
    "    stopfractions = pickle.load(open(stopfractionsname,'rb'))\n",
    "\n",
    "    total_triptime = predict_total_triptime(route,direction,datetime_departure,feels_like,weather_main)\n",
    "    \n",
    "    stopA_frac = stopfractions[stopfractions['LINEID']==route][stopfractions['DIRECTION']==direction][stopfractions['STOPPOINTID']==stopA]['TRIP_FRAC'].iloc[0]\n",
    "    stopB_frac = stopfractions[stopfractions['LINEID']==route][stopfractions['DIRECTION']==direction][stopfractions['STOPPOINTID']==stopB]['TRIP_FRAC'].iloc[0]\n",
    "    \n",
    "    fraction = stopB_frac - stopA_frac\n",
    "    return total_triptime*fraction\n",
    "    \n",
    "def predict_total_triptime(route,direction,datetime_departure,feels_like,weather_main):\n",
    "    model = find_model(route,direction)\n",
    "    \n",
    "    feels_like = normalize(feels_like,model['max_feels_like'],model['min_feels_like'])\n",
    "    \n",
    "    time_in_seconds = datetime_departure.hour*60*60 + datetime_departure.minute*60 + datetime_departure.second\n",
    "    plannedtime_dep_cos = np.cos(2*np.pi*time_in_seconds/(60*60*24))\n",
    "    plannedtime_dep_sin = np.sin(2*np.pi*time_in_seconds/(60*60*24))\n",
    "    month = datetime_departure.month\n",
    "    weekday = datetime_departure.weekday()\n",
    "    weekend = True if weekday in [5,6] else False\n",
    "    rush_hour = (25200 < time_in_seconds < 32400) or (57600 < time_in_seconds < 68400)\n",
    "    \n",
    "    params = {\n",
    "        'RUSH_HOUR_' + str(rush_hour):1,\n",
    "        'PLANNEDTIME_DEP_COS':plannedtime_dep_cos,\n",
    "        'PLANNEDTIME_DEP_SIN':plannedtime_dep_sin,\n",
    "        'feels_like':feels_like,\n",
    "        'MONTH_' + str(month):1,\n",
    "        'WEEKDAY_' + str(weekday):1,\n",
    "        'WEEKEND_' + str(weekend):1,\n",
    "        'weather_main_' + str(weather_main):1\n",
    "    }\n",
    "\n",
    "    all_params = [feature for feature in model['columns']]\n",
    "    for feature in all_params:\n",
    "        if feature not in params:\n",
    "            params[feature] = 0\n",
    "    \n",
    "    d = pd.DataFrame(params,index=[0],columns=model['columns'])\n",
    "    \n",
    "#     prediction = unnormalize(model['model'].predict(d), model['max_trip'], model['min_trip'])\n",
    "    prediction = model['model'].predict(d)\n",
    "    return prediction\n",
    "\n",
    "def predict_total_triptimes(route,direction,datetimes_departure,feels_like,weather_main):\n",
    "    model = find_model(route,direction)\n",
    "    \n",
    "    feels_like = normalize(feels_like,model['max_feels_like'],model['min_feels_like'])\n",
    "    \n",
    "    params = {feature:[0]*len(datetimes_departure) for feature in model['columns']}\n",
    "    \n",
    "    for i, dt in enumerate(datetimes_departure):\n",
    "        time_in_seconds = dt.hour*60*60 + dt.minute*60 + dt.second\n",
    "        plannedtime_dep_cos = np.cos(2*np.pi*time_in_seconds/(60*60*24))\n",
    "        plannedtime_dep_sin = np.sin(2*np.pi*time_in_seconds/(60*60*24))\n",
    "        month = dt.month\n",
    "        weekday = dt.weekday()\n",
    "        weekend = True if weekday in [5,6] else False\n",
    "        rush_hour = (25200 < time_in_seconds < 32400) or (57600 < time_in_seconds < 68400)\n",
    "    \n",
    "        params['RUSH_HOUR_' + str(rush_hour)][i] = 1\n",
    "        params['PLANNEDTIME_DEP_COS'][i] = plannedtime_dep_cos\n",
    "        params['PLANNEDTIME_DEP_SIN'][i] = plannedtime_dep_sin\n",
    "        params['feels_like'][i] = 1\n",
    "        params['MONTH_' + str(month)][i] = 1\n",
    "        params['WEEKDAY_' + str(weekday)][i] = 1\n",
    "        params['WEEKEND_' + str(weekend)][i] = 1\n",
    "        params['weather_main_' + str(weather_main)][i] = 1\n",
    "    \n",
    "    d = pd.DataFrame(params,index=[i for i in range(len(datetimes_departure))],columns=model['columns'])\n",
    "    \n",
    "    predictions = unnormalize(model['model'].predict(d), model['max_trip'], model['min_trip'])\n",
    "    \n",
    "    return predictions\n",
    "\n",
    "def unnormalize(x_norm,x_max,x_min):\n",
    "    return x_norm*(x_max - x_min) + x_min\n",
    "\n",
    "def normalize(x,x_max,x_min):\n",
    "    return (x - x_min)/(x_max - x_min)\n",
    "\n",
    "def find_model(route,direction):\n",
    "    filename = \"models_SVR/models.pkl\"\n",
    "    models = pickle.load(open(filename, \"rb\"))\n",
    "    for model in models:\n",
    "        if model['route'] == route and model['direction'] == direction:\n",
    "            return model\n",
    "        \n",
    "def get_5day_forcast():\n",
    "    import requests\n",
    "\n",
    "    \"\"\"Scrapes weather data from openweathermap.org\"\"\"\n",
    "    \n",
    "    API_KEY = \"16fb93e92d3bd8aefd9b647c1a8f6acf\"\n",
    "    URL = \"http://api.openweathermap.org/data/2.5/forecast?q=Dublin,ie&appid=\" + API_KEY\n",
    "    \n",
    "#     time = get_datetime()\n",
    "    try:\n",
    "        r = requests.get(url = URL)\n",
    "    except: \n",
    "        print(\"Scraping error: data not collected.\")\n",
    "        return None\n",
    "    \n",
    "    weather = r.json()\n",
    "    return weather"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'today' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-3-2b3130b4e757>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0md\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpredict_total_triptime\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'46A'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mtoday\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'Drizzle'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'today' is not defined"
     ]
    }
   ],
   "source": [
    "d = predict_total_triptime('46A',1,today,0,'Drizzle')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 239,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.39143826])"
      ]
     },
     "execution_count": 239,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import datetime\n",
    "\n",
    "today = datetime.datetime.now()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "weekday = today.weekday()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 242,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 242,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "weekday"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 243,
   "metadata": {},
   "outputs": [],
   "source": [
    "weekend = True if weekday in [5,6] else False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 244,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 244,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "weekend"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 245,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'True'"
      ]
     },
     "execution_count": 245,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str(True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Mesel\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:31: UserWarning: Boolean Series key will be reindexed to match DataFrame index.\n",
      "C:\\Users\\Mesel\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:32: UserWarning: Boolean Series key will be reindexed to match DataFrame index.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "325.8525642888964"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# predict_fractional_triptime('1',1,today,0,'Drizzle',48,44)\n",
    "\n",
    "predict_travel_time('1',1,today,20,'Drizzle',48,44)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>DATASOURCE</th>\n",
       "      <th>DAYOFSERVICE</th>\n",
       "      <th>TRIPID</th>\n",
       "      <th>LINEID</th>\n",
       "      <th>ROUTEID</th>\n",
       "      <th>DIRECTION</th>\n",
       "      <th>PLANNEDTIME_ARR</th>\n",
       "      <th>PLANNEDTIME_DEP</th>\n",
       "      <th>ACTUALTIME_ARR</th>\n",
       "      <th>ACTUALTIME_DEP</th>\n",
       "      <th>BASIN</th>\n",
       "      <th>TENDERLOT</th>\n",
       "      <th>SUPPRESSED</th>\n",
       "      <th>JUSTIFICATIONID</th>\n",
       "      <th>LASTUPDATE</th>\n",
       "      <th>NOTE</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>DB</td>\n",
       "      <td>07-FEB-18 00:00:00</td>\n",
       "      <td>6253783</td>\n",
       "      <td>68</td>\n",
       "      <td>68_80</td>\n",
       "      <td>1</td>\n",
       "      <td>87245</td>\n",
       "      <td>84600</td>\n",
       "      <td>87524.0</td>\n",
       "      <td>84600.0</td>\n",
       "      <td>BasDef</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>28-FEB-18 12:05:11</td>\n",
       "      <td>,2967409,</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>DB</td>\n",
       "      <td>07-FEB-18 00:00:00</td>\n",
       "      <td>6262138</td>\n",
       "      <td>25B</td>\n",
       "      <td>25B_271</td>\n",
       "      <td>2</td>\n",
       "      <td>30517</td>\n",
       "      <td>26460</td>\n",
       "      <td>32752.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>BasDef</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>28-FEB-18 12:05:11</td>\n",
       "      <td>,2580260,</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>DB</td>\n",
       "      <td>07-FEB-18 00:00:00</td>\n",
       "      <td>6254942</td>\n",
       "      <td>45A</td>\n",
       "      <td>45A_70</td>\n",
       "      <td>2</td>\n",
       "      <td>35512</td>\n",
       "      <td>32100</td>\n",
       "      <td>36329.0</td>\n",
       "      <td>32082.0</td>\n",
       "      <td>BasDef</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>28-FEB-18 12:05:11</td>\n",
       "      <td>,2448968,</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>DB</td>\n",
       "      <td>07-FEB-18 00:00:00</td>\n",
       "      <td>6259460</td>\n",
       "      <td>25A</td>\n",
       "      <td>25A_273</td>\n",
       "      <td>1</td>\n",
       "      <td>57261</td>\n",
       "      <td>54420</td>\n",
       "      <td>58463.0</td>\n",
       "      <td>54443.0</td>\n",
       "      <td>BasDef</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>28-FEB-18 12:05:11</td>\n",
       "      <td>,3094242,</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>DB</td>\n",
       "      <td>07-FEB-18 00:00:00</td>\n",
       "      <td>6253175</td>\n",
       "      <td>14</td>\n",
       "      <td>14_15</td>\n",
       "      <td>1</td>\n",
       "      <td>85383</td>\n",
       "      <td>81600</td>\n",
       "      <td>84682.0</td>\n",
       "      <td>81608.0</td>\n",
       "      <td>BasDef</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>28-FEB-18 12:05:11</td>\n",
       "      <td>,2526331,</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  DATASOURCE        DAYOFSERVICE   TRIPID LINEID  ROUTEID  DIRECTION  \\\n",
       "0         DB  07-FEB-18 00:00:00  6253783     68    68_80          1   \n",
       "1         DB  07-FEB-18 00:00:00  6262138    25B  25B_271          2   \n",
       "2         DB  07-FEB-18 00:00:00  6254942    45A   45A_70          2   \n",
       "3         DB  07-FEB-18 00:00:00  6259460    25A  25A_273          1   \n",
       "4         DB  07-FEB-18 00:00:00  6253175     14    14_15          1   \n",
       "\n",
       "   PLANNEDTIME_ARR  PLANNEDTIME_DEP  ACTUALTIME_ARR  ACTUALTIME_DEP   BASIN  \\\n",
       "0            87245            84600         87524.0         84600.0  BasDef   \n",
       "1            30517            26460         32752.0             NaN  BasDef   \n",
       "2            35512            32100         36329.0         32082.0  BasDef   \n",
       "3            57261            54420         58463.0         54443.0  BasDef   \n",
       "4            85383            81600         84682.0         81608.0  BasDef   \n",
       "\n",
       "   TENDERLOT  SUPPRESSED  JUSTIFICATIONID          LASTUPDATE       NOTE  \n",
       "0        NaN         NaN              NaN  28-FEB-18 12:05:11  ,2967409,  \n",
       "1        NaN         NaN              NaN  28-FEB-18 12:05:11  ,2580260,  \n",
       "2        NaN         NaN              NaN  28-FEB-18 12:05:11  ,2448968,  \n",
       "3        NaN         NaN              NaN  28-FEB-18 12:05:11  ,3094242,  \n",
       "4        NaN         NaN              NaN  28-FEB-18 12:05:11  ,2526331,  "
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dftrips = pd.read_csv('../../data/rt_trips_DB_2018.txt',sep=';')\n",
    "\n",
    "dftrips.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [],
   "source": [
    "dftrips.drop(['BASIN','TENDERLOT','SUPPRESSED','JUSTIFICATIONID','LASTUPDATE','NOTE','ACTUALTIME_ARR','ACTUALTIME_DEP','ROUTEID','TRIPID','DATASOURCE'],axis=1,inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [],
   "source": [
    "dftrips['DAYOFSERVICE'] = dftrips['DAYOFSERVICE'].astype('datetime64')\n",
    "dftrips['LINEID'] = dftrips['LINEID'].astype('category')\n",
    "dftrips['DIRECTION'] = dftrips['DIRECTION'].astype('category')\n",
    "\n",
    "dftrips['WEEKDAY'] = dftrips['DAYOFSERVICE'].dt.weekday"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>DAYOFSERVICE</th>\n",
       "      <th>LINEID</th>\n",
       "      <th>DIRECTION</th>\n",
       "      <th>PLANNEDTIME_ARR</th>\n",
       "      <th>PLANNEDTIME_DEP</th>\n",
       "      <th>WEEKDAY</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-02-07</td>\n",
       "      <td>68</td>\n",
       "      <td>1</td>\n",
       "      <td>87245</td>\n",
       "      <td>84600</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-02-07</td>\n",
       "      <td>25B</td>\n",
       "      <td>2</td>\n",
       "      <td>30517</td>\n",
       "      <td>26460</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-02-07</td>\n",
       "      <td>45A</td>\n",
       "      <td>2</td>\n",
       "      <td>35512</td>\n",
       "      <td>32100</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2018-02-07</td>\n",
       "      <td>25A</td>\n",
       "      <td>1</td>\n",
       "      <td>57261</td>\n",
       "      <td>54420</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2018-02-07</td>\n",
       "      <td>14</td>\n",
       "      <td>1</td>\n",
       "      <td>85383</td>\n",
       "      <td>81600</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  DAYOFSERVICE LINEID DIRECTION  PLANNEDTIME_ARR  PLANNEDTIME_DEP  WEEKDAY\n",
       "0   2018-02-07     68         1            87245            84600        2\n",
       "1   2018-02-07    25B         2            30517            26460        2\n",
       "2   2018-02-07    45A         2            35512            32100        2\n",
       "3   2018-02-07    25A         1            57261            54420        2\n",
       "4   2018-02-07     14         1            85383            81600        2"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dftrips.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Mesel\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:6: UserWarning: Boolean Series key will be reindexed to match DataFrame index.\n",
      "  \n",
      "C:\\Users\\Mesel\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:8: UserWarning: Boolean Series key will be reindexed to match DataFrame index.\n",
      "  \n"
     ]
    }
   ],
   "source": [
    "departure_times = dict()\n",
    "for route in dftrips['LINEID'].unique():\n",
    "    departure_times[route] = dict()\n",
    "    for direction in dftrips[dftrips['LINEID'] == route]['DIRECTION'].unique():\n",
    "        departure_times[route][direction] = dict()\n",
    "        for weekday in dftrips[dftrips['LINEID'] == route][dftrips['DIRECTION'] == direction]['WEEKDAY'].unique():\n",
    "            departure_times[route][direction][weekday] = list()\n",
    "            for plannedtime_dep in dftrips[dftrips['LINEID'] == route][dftrips['DIRECTION'] == direction][dftrips['WEEKDAY'] == weekday]['PLANNEDTIME_DEP'].unique():\n",
    "                departure_times[route][direction][weekday] += [plannedtime_dep]\n",
    "            departure_times[route][direction][weekday] = sorted(departure_times[route][direction][weekday])\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 323,
   "metadata": {},
   "outputs": [],
   "source": [
    "pickle.dump(departure_times,open('departure_times.pkl','wb'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[18900, 21960, 22080, 22500, 22560, 23040, 23520, 23580, 24000, 24120, 24480, 24660, 24960, 25200, 25440, 25740, 25920, 26100, 26280, 26400, 26820, 26880, 27000, 27360, 27600, 27840, 27900, 28320, 28440, 28800, 28980, 29280, 29400, 29520, 29760, 30000, 30060, 30240, 30600, 30720, 31140, 31200, 31680, 31800, 32100, 32160, 32220, 32400, 32640, 32760, 33000, 33120, 33300, 33600, 33840, 34080, 34200, 34380, 34560, 34800, 34920, 35040, 35100, 35400, 35460, 35520, 36000, 36480, 36540, 36600, 36900, 36960, 37080, 37200, 37440, 37620, 37800, 37920, 38160, 38400, 38700, 38880, 39000, 39240, 39360, 39600, 39780, 39840, 40200, 40320, 40500, 40800, 40860, 41280, 41400, 41760, 41940, 42000, 42240, 42300, 42480, 42600, 42720, 43020, 43200, 43560, 43680, 43800, 44100, 44160, 44400, 44640, 45000, 45120, 45180, 45600, 45720, 46080, 46200, 46260, 46560, 46800, 47040, 47340, 47400, 47520, 47880, 48000, 48420, 48480, 48600, 48960, 49200, 49440, 49500, 49800, 49920, 50040, 50400, 50580, 50880, 51000, 51120, 51360, 51600, 51660, 51840, 52200, 52320, 52740, 52800, 53280, 53400, 53760, 53820, 54000, 54240, 54360, 54600, 54720, 54900, 55200, 55440, 55680, 55800, 55980, 56160, 56400, 56520, 56640, 57000, 57060, 57120, 57600, 58080, 58140, 58200, 58560, 58680, 58800, 59040, 59220, 59400, 59520, 59760, 60000, 60300, 60480, 60600, 60780, 60840, 60960, 61200, 61380, 61440, 61800, 61920, 62280, 62400, 62460, 62880, 63000, 63360, 63540, 63600, 63840, 63900, 64080, 64200, 64320, 64620, 64800, 65160, 65280, 65400, 65700, 65760, 66000, 66240, 66600, 66720, 66780, 67200, 67320, 67680, 67800, 67860, 68160, 68400, 68580, 69000, 69300, 69600, 70200, 70800, 71100, 71400, 72000, 72600, 72900, 73200, 73800, 74400, 74700, 75000, 75600, 76500, 76800, 77400, 78000, 78300, 79200, 80100, 80400, 81000, 81600, 81900, 82800, 83700, 84600]\n"
     ]
    }
   ],
   "source": [
    "print(departure_times['46A'][1][0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 247,
   "metadata": {},
   "outputs": [],
   "source": [
    "dt = datetime.datetime(2020,12,12,14,30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 248,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 249,
   "metadata": {},
   "outputs": [],
   "source": [
    "forecast = get_5day_forcast()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 250,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'cod': '200',\n",
       " 'message': 0,\n",
       " 'cnt': 40,\n",
       " 'list': [{'dt': 1595505600,\n",
       "   'main': {'temp': 290.35,\n",
       "    'feels_like': 288.9,\n",
       "    'temp_min': 290.35,\n",
       "    'temp_max': 291.06,\n",
       "    'pressure': 1012,\n",
       "    'sea_level': 1013,\n",
       "    'grnd_level': 1011,\n",
       "    'humidity': 86,\n",
       "    'temp_kf': -0.71},\n",
       "   'weather': [{'id': 804,\n",
       "     'main': 'Clouds',\n",
       "     'description': 'overcast clouds',\n",
       "     'icon': '04d'}],\n",
       "   'clouds': {'all': 87},\n",
       "   'wind': {'speed': 4.3, 'deg': 266},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0.79,\n",
       "   'sys': {'pod': 'd'},\n",
       "   'dt_txt': '2020-07-23 12:00:00'},\n",
       "  {'dt': 1595516400,\n",
       "   'main': {'temp': 290.96,\n",
       "    'feels_like': 289.72,\n",
       "    'temp_min': 290.96,\n",
       "    'temp_max': 291.31,\n",
       "    'pressure': 1012,\n",
       "    'sea_level': 1012,\n",
       "    'grnd_level': 1010,\n",
       "    'humidity': 81,\n",
       "    'temp_kf': -0.35},\n",
       "   'weather': [{'id': 500,\n",
       "     'main': 'Rain',\n",
       "     'description': 'light rain',\n",
       "     'icon': '10d'}],\n",
       "   'clouds': {'all': 90},\n",
       "   'wind': {'speed': 3.83, 'deg': 281},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0.24,\n",
       "   'rain': {'3h': 0.2},\n",
       "   'sys': {'pod': 'd'},\n",
       "   'dt_txt': '2020-07-23 15:00:00'},\n",
       "  {'dt': 1595527200,\n",
       "   'main': {'temp': 290.85,\n",
       "    'feels_like': 289.45,\n",
       "    'temp_min': 290.85,\n",
       "    'temp_max': 290.93,\n",
       "    'pressure': 1012,\n",
       "    'sea_level': 1012,\n",
       "    'grnd_level': 1010,\n",
       "    'humidity': 78,\n",
       "    'temp_kf': -0.08},\n",
       "   'weather': [{'id': 500,\n",
       "     'main': 'Rain',\n",
       "     'description': 'light rain',\n",
       "     'icon': '10d'}],\n",
       "   'clouds': {'all': 89},\n",
       "   'wind': {'speed': 3.71, 'deg': 284},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0.18,\n",
       "   'rain': {'3h': 0.17},\n",
       "   'sys': {'pod': 'd'},\n",
       "   'dt_txt': '2020-07-23 18:00:00'},\n",
       "  {'dt': 1595538000,\n",
       "   'main': {'temp': 287.74,\n",
       "    'feels_like': 286.77,\n",
       "    'temp_min': 287.72,\n",
       "    'temp_max': 287.74,\n",
       "    'pressure': 1013,\n",
       "    'sea_level': 1013,\n",
       "    'grnd_level': 1011,\n",
       "    'humidity': 90,\n",
       "    'temp_kf': 0.02},\n",
       "   'weather': [{'id': 803,\n",
       "     'main': 'Clouds',\n",
       "     'description': 'broken clouds',\n",
       "     'icon': '04n'}],\n",
       "   'clouds': {'all': 80},\n",
       "   'wind': {'speed': 2.71, 'deg': 261},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0,\n",
       "   'sys': {'pod': 'n'},\n",
       "   'dt_txt': '2020-07-23 21:00:00'},\n",
       "  {'dt': 1595548800,\n",
       "   'main': {'temp': 285.73,\n",
       "    'feels_like': 284.46,\n",
       "    'temp_min': 285.73,\n",
       "    'temp_max': 285.73,\n",
       "    'pressure': 1013,\n",
       "    'sea_level': 1013,\n",
       "    'grnd_level': 1011,\n",
       "    'humidity': 92,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 802,\n",
       "     'main': 'Clouds',\n",
       "     'description': 'scattered clouds',\n",
       "     'icon': '03n'}],\n",
       "   'clouds': {'all': 43},\n",
       "   'wind': {'speed': 2.41, 'deg': 268},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0,\n",
       "   'sys': {'pod': 'n'},\n",
       "   'dt_txt': '2020-07-24 00:00:00'},\n",
       "  {'dt': 1595559600,\n",
       "   'main': {'temp': 284.52,\n",
       "    'feels_like': 282.69,\n",
       "    'temp_min': 284.52,\n",
       "    'temp_max': 284.52,\n",
       "    'pressure': 1012,\n",
       "    'sea_level': 1012,\n",
       "    'grnd_level': 1010,\n",
       "    'humidity': 92,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 802,\n",
       "     'main': 'Clouds',\n",
       "     'description': 'scattered clouds',\n",
       "     'icon': '03n'}],\n",
       "   'clouds': {'all': 30},\n",
       "   'wind': {'speed': 2.73, 'deg': 261},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0,\n",
       "   'sys': {'pod': 'n'},\n",
       "   'dt_txt': '2020-07-24 03:00:00'},\n",
       "  {'dt': 1595570400,\n",
       "   'main': {'temp': 285.44,\n",
       "    'feels_like': 283.99,\n",
       "    'temp_min': 285.44,\n",
       "    'temp_max': 285.44,\n",
       "    'pressure': 1012,\n",
       "    'sea_level': 1012,\n",
       "    'grnd_level': 1010,\n",
       "    'humidity': 90,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 802,\n",
       "     'main': 'Clouds',\n",
       "     'description': 'scattered clouds',\n",
       "     'icon': '03d'}],\n",
       "   'clouds': {'all': 36},\n",
       "   'wind': {'speed': 2.41, 'deg': 254},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0,\n",
       "   'sys': {'pod': 'd'},\n",
       "   'dt_txt': '2020-07-24 06:00:00'},\n",
       "  {'dt': 1595581200,\n",
       "   'main': {'temp': 289.92,\n",
       "    'feels_like': 288.99,\n",
       "    'temp_min': 289.92,\n",
       "    'temp_max': 289.92,\n",
       "    'pressure': 1011,\n",
       "    'sea_level': 1011,\n",
       "    'grnd_level': 1010,\n",
       "    'humidity': 74,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 803,\n",
       "     'main': 'Clouds',\n",
       "     'description': 'broken clouds',\n",
       "     'icon': '04d'}],\n",
       "   'clouds': {'all': 64},\n",
       "   'wind': {'speed': 2.26, 'deg': 243},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0,\n",
       "   'sys': {'pod': 'd'},\n",
       "   'dt_txt': '2020-07-24 09:00:00'},\n",
       "  {'dt': 1595592000,\n",
       "   'main': {'temp': 292.28,\n",
       "    'feels_like': 291.77,\n",
       "    'temp_min': 292.28,\n",
       "    'temp_max': 292.28,\n",
       "    'pressure': 1011,\n",
       "    'sea_level': 1011,\n",
       "    'grnd_level': 1009,\n",
       "    'humidity': 66,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 803,\n",
       "     'main': 'Clouds',\n",
       "     'description': 'broken clouds',\n",
       "     'icon': '04d'}],\n",
       "   'clouds': {'all': 82},\n",
       "   'wind': {'speed': 1.89, 'deg': 199},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0,\n",
       "   'sys': {'pod': 'd'},\n",
       "   'dt_txt': '2020-07-24 12:00:00'},\n",
       "  {'dt': 1595602800,\n",
       "   'main': {'temp': 292.97,\n",
       "    'feels_like': 292.25,\n",
       "    'temp_min': 292.97,\n",
       "    'temp_max': 292.97,\n",
       "    'pressure': 1009,\n",
       "    'sea_level': 1009,\n",
       "    'grnd_level': 1007,\n",
       "    'humidity': 69,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 804,\n",
       "     'main': 'Clouds',\n",
       "     'description': 'overcast clouds',\n",
       "     'icon': '04d'}],\n",
       "   'clouds': {'all': 100},\n",
       "   'wind': {'speed': 2.81, 'deg': 212},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0.04,\n",
       "   'sys': {'pod': 'd'},\n",
       "   'dt_txt': '2020-07-24 15:00:00'},\n",
       "  {'dt': 1595613600,\n",
       "   'main': {'temp': 290.03,\n",
       "    'feels_like': 289.99,\n",
       "    'temp_min': 290.03,\n",
       "    'temp_max': 290.03,\n",
       "    'pressure': 1007,\n",
       "    'sea_level': 1007,\n",
       "    'grnd_level': 1005,\n",
       "    'humidity': 91,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 501,\n",
       "     'main': 'Rain',\n",
       "     'description': 'moderate rain',\n",
       "     'icon': '10d'}],\n",
       "   'clouds': {'all': 100},\n",
       "   'wind': {'speed': 2.58, 'deg': 204},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0.95,\n",
       "   'rain': {'3h': 3.12},\n",
       "   'sys': {'pod': 'd'},\n",
       "   'dt_txt': '2020-07-24 18:00:00'},\n",
       "  {'dt': 1595624400,\n",
       "   'main': {'temp': 289.26,\n",
       "    'feels_like': 287.83,\n",
       "    'temp_min': 289.26,\n",
       "    'temp_max': 289.26,\n",
       "    'pressure': 1006,\n",
       "    'sea_level': 1006,\n",
       "    'grnd_level': 1005,\n",
       "    'humidity': 92,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 500,\n",
       "     'main': 'Rain',\n",
       "     'description': 'light rain',\n",
       "     'icon': '10n'}],\n",
       "   'clouds': {'all': 100},\n",
       "   'wind': {'speed': 4.25, 'deg': 238},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0.95,\n",
       "   'rain': {'3h': 1.23},\n",
       "   'sys': {'pod': 'n'},\n",
       "   'dt_txt': '2020-07-24 21:00:00'},\n",
       "  {'dt': 1595635200,\n",
       "   'main': {'temp': 288.61,\n",
       "    'feels_like': 286.88,\n",
       "    'temp_min': 288.61,\n",
       "    'temp_max': 288.61,\n",
       "    'pressure': 1005,\n",
       "    'sea_level': 1005,\n",
       "    'grnd_level': 1003,\n",
       "    'humidity': 91,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 500,\n",
       "     'main': 'Rain',\n",
       "     'description': 'light rain',\n",
       "     'icon': '10n'}],\n",
       "   'clouds': {'all': 92},\n",
       "   'wind': {'speed': 4.27, 'deg': 233},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0.85,\n",
       "   'rain': {'3h': 0.66},\n",
       "   'sys': {'pod': 'n'},\n",
       "   'dt_txt': '2020-07-25 00:00:00'},\n",
       "  {'dt': 1595646000,\n",
       "   'main': {'temp': 288.36,\n",
       "    'feels_like': 287.52,\n",
       "    'temp_min': 288.36,\n",
       "    'temp_max': 288.36,\n",
       "    'pressure': 1004,\n",
       "    'sea_level': 1004,\n",
       "    'grnd_level': 1002,\n",
       "    'humidity': 91,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 500,\n",
       "     'main': 'Rain',\n",
       "     'description': 'light rain',\n",
       "     'icon': '10n'}],\n",
       "   'clouds': {'all': 97},\n",
       "   'wind': {'speed': 2.89, 'deg': 220},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0.47,\n",
       "   'rain': {'3h': 0.17},\n",
       "   'sys': {'pod': 'n'},\n",
       "   'dt_txt': '2020-07-25 03:00:00'},\n",
       "  {'dt': 1595656800,\n",
       "   'main': {'temp': 288.83,\n",
       "    'feels_like': 287.05,\n",
       "    'temp_min': 288.83,\n",
       "    'temp_max': 288.83,\n",
       "    'pressure': 1004,\n",
       "    'sea_level': 1004,\n",
       "    'grnd_level': 1002,\n",
       "    'humidity': 91,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 500,\n",
       "     'main': 'Rain',\n",
       "     'description': 'light rain',\n",
       "     'icon': '10d'}],\n",
       "   'clouds': {'all': 98},\n",
       "   'wind': {'speed': 4.45, 'deg': 255},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0.69,\n",
       "   'rain': {'3h': 0.18},\n",
       "   'sys': {'pod': 'd'},\n",
       "   'dt_txt': '2020-07-25 06:00:00'},\n",
       "  {'dt': 1595667600,\n",
       "   'main': {'temp': 289.47,\n",
       "    'feels_like': 287.18,\n",
       "    'temp_min': 289.47,\n",
       "    'temp_max': 289.47,\n",
       "    'pressure': 1004,\n",
       "    'sea_level': 1004,\n",
       "    'grnd_level': 1002,\n",
       "    'humidity': 80,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 804,\n",
       "     'main': 'Clouds',\n",
       "     'description': 'overcast clouds',\n",
       "     'icon': '04d'}],\n",
       "   'clouds': {'all': 100},\n",
       "   'wind': {'speed': 4.54, 'deg': 268},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0.22,\n",
       "   'sys': {'pod': 'd'},\n",
       "   'dt_txt': '2020-07-25 09:00:00'},\n",
       "  {'dt': 1595678400,\n",
       "   'main': {'temp': 292.31,\n",
       "    'feels_like': 290.44,\n",
       "    'temp_min': 292.31,\n",
       "    'temp_max': 292.31,\n",
       "    'pressure': 1005,\n",
       "    'sea_level': 1005,\n",
       "    'grnd_level': 1003,\n",
       "    'humidity': 68,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 500,\n",
       "     'main': 'Rain',\n",
       "     'description': 'light rain',\n",
       "     'icon': '10d'}],\n",
       "   'clouds': {'all': 100},\n",
       "   'wind': {'speed': 4.05, 'deg': 273},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0.32,\n",
       "   'rain': {'3h': 0.19},\n",
       "   'sys': {'pod': 'd'},\n",
       "   'dt_txt': '2020-07-25 12:00:00'},\n",
       "  {'dt': 1595689200,\n",
       "   'main': {'temp': 292.15,\n",
       "    'feels_like': 290.34,\n",
       "    'temp_min': 292.15,\n",
       "    'temp_max': 292.15,\n",
       "    'pressure': 1004,\n",
       "    'sea_level': 1004,\n",
       "    'grnd_level': 1002,\n",
       "    'humidity': 70,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 500,\n",
       "     'main': 'Rain',\n",
       "     'description': 'light rain',\n",
       "     'icon': '10d'}],\n",
       "   'clouds': {'all': 100},\n",
       "   'wind': {'speed': 4.1, 'deg': 276},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0.94,\n",
       "   'rain': {'3h': 0.97},\n",
       "   'sys': {'pod': 'd'},\n",
       "   'dt_txt': '2020-07-25 15:00:00'},\n",
       "  {'dt': 1595700000,\n",
       "   'main': {'temp': 290.24,\n",
       "    'feels_like': 289.18,\n",
       "    'temp_min': 290.24,\n",
       "    'temp_max': 290.24,\n",
       "    'pressure': 1004,\n",
       "    'sea_level': 1004,\n",
       "    'grnd_level': 1002,\n",
       "    'humidity': 81,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 500,\n",
       "     'main': 'Rain',\n",
       "     'description': 'light rain',\n",
       "     'icon': '10d'}],\n",
       "   'clouds': {'all': 100},\n",
       "   'wind': {'speed': 3.22, 'deg': 294},\n",
       "   'visibility': 10000,\n",
       "   'pop': 1,\n",
       "   'rain': {'3h': 1.84},\n",
       "   'sys': {'pod': 'd'},\n",
       "   'dt_txt': '2020-07-25 18:00:00'},\n",
       "  {'dt': 1595710800,\n",
       "   'main': {'temp': 287.14,\n",
       "    'feels_like': 285.82,\n",
       "    'temp_min': 287.14,\n",
       "    'temp_max': 287.14,\n",
       "    'pressure': 1004,\n",
       "    'sea_level': 1004,\n",
       "    'grnd_level': 1003,\n",
       "    'humidity': 88,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 500,\n",
       "     'main': 'Rain',\n",
       "     'description': 'light rain',\n",
       "     'icon': '10n'}],\n",
       "   'clouds': {'all': 100},\n",
       "   'wind': {'speed': 2.79, 'deg': 272},\n",
       "   'visibility': 10000,\n",
       "   'pop': 1,\n",
       "   'rain': {'3h': 1.24},\n",
       "   'sys': {'pod': 'n'},\n",
       "   'dt_txt': '2020-07-25 21:00:00'},\n",
       "  {'dt': 1595721600,\n",
       "   'main': {'temp': 284.99,\n",
       "    'feels_like': 282.75,\n",
       "    'temp_min': 284.99,\n",
       "    'temp_max': 284.99,\n",
       "    'pressure': 1005,\n",
       "    'sea_level': 1005,\n",
       "    'grnd_level': 1003,\n",
       "    'humidity': 91,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 500,\n",
       "     'main': 'Rain',\n",
       "     'description': 'light rain',\n",
       "     'icon': '10n'}],\n",
       "   'clouds': {'all': 85},\n",
       "   'wind': {'speed': 3.43, 'deg': 264},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0.96,\n",
       "   'rain': {'3h': 0.11},\n",
       "   'sys': {'pod': 'n'},\n",
       "   'dt_txt': '2020-07-26 00:00:00'},\n",
       "  {'dt': 1595732400,\n",
       "   'main': {'temp': 284.46,\n",
       "    'feels_like': 281.85,\n",
       "    'temp_min': 284.46,\n",
       "    'temp_max': 284.46,\n",
       "    'pressure': 1005,\n",
       "    'sea_level': 1005,\n",
       "    'grnd_level': 1003,\n",
       "    'humidity': 92,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 801,\n",
       "     'main': 'Clouds',\n",
       "     'description': 'few clouds',\n",
       "     'icon': '02n'}],\n",
       "   'clouds': {'all': 14},\n",
       "   'wind': {'speed': 3.82, 'deg': 254},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0,\n",
       "   'sys': {'pod': 'n'},\n",
       "   'dt_txt': '2020-07-26 03:00:00'},\n",
       "  {'dt': 1595743200,\n",
       "   'main': {'temp': 284.98,\n",
       "    'feels_like': 282.54,\n",
       "    'temp_min': 284.98,\n",
       "    'temp_max': 284.98,\n",
       "    'pressure': 1006,\n",
       "    'sea_level': 1006,\n",
       "    'grnd_level': 1004,\n",
       "    'humidity': 90,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 801,\n",
       "     'main': 'Clouds',\n",
       "     'description': 'few clouds',\n",
       "     'icon': '02d'}],\n",
       "   'clouds': {'all': 17},\n",
       "   'wind': {'speed': 3.65, 'deg': 241},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0,\n",
       "   'sys': {'pod': 'd'},\n",
       "   'dt_txt': '2020-07-26 06:00:00'},\n",
       "  {'dt': 1595754000,\n",
       "   'main': {'temp': 288.53,\n",
       "    'feels_like': 285.8,\n",
       "    'temp_min': 288.53,\n",
       "    'temp_max': 288.53,\n",
       "    'pressure': 1007,\n",
       "    'sea_level': 1007,\n",
       "    'grnd_level': 1005,\n",
       "    'humidity': 77,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 803,\n",
       "     'main': 'Clouds',\n",
       "     'description': 'broken clouds',\n",
       "     'icon': '04d'}],\n",
       "   'clouds': {'all': 74},\n",
       "   'wind': {'speed': 4.51, 'deg': 232},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0,\n",
       "   'sys': {'pod': 'd'},\n",
       "   'dt_txt': '2020-07-26 09:00:00'},\n",
       "  {'dt': 1595764800,\n",
       "   'main': {'temp': 291,\n",
       "    'feels_like': 288,\n",
       "    'temp_min': 291,\n",
       "    'temp_max': 291,\n",
       "    'pressure': 1007,\n",
       "    'sea_level': 1007,\n",
       "    'grnd_level': 1005,\n",
       "    'humidity': 68,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 500,\n",
       "     'main': 'Rain',\n",
       "     'description': 'light rain',\n",
       "     'icon': '10d'}],\n",
       "   'clouds': {'all': 87},\n",
       "   'wind': {'speed': 5.11, 'deg': 236},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0.46,\n",
       "   'rain': {'3h': 0.38},\n",
       "   'sys': {'pod': 'd'},\n",
       "   'dt_txt': '2020-07-26 12:00:00'},\n",
       "  {'dt': 1595775600,\n",
       "   'main': {'temp': 290.71,\n",
       "    'feels_like': 286.78,\n",
       "    'temp_min': 290.71,\n",
       "    'temp_max': 290.71,\n",
       "    'pressure': 1007,\n",
       "    'sea_level': 1007,\n",
       "    'grnd_level': 1005,\n",
       "    'humidity': 70,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 500,\n",
       "     'main': 'Rain',\n",
       "     'description': 'light rain',\n",
       "     'icon': '10d'}],\n",
       "   'clouds': {'all': 100},\n",
       "   'wind': {'speed': 6.51, 'deg': 255},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0.97,\n",
       "   'rain': {'3h': 0.73},\n",
       "   'sys': {'pod': 'd'},\n",
       "   'dt_txt': '2020-07-26 15:00:00'},\n",
       "  {'dt': 1595786400,\n",
       "   'main': {'temp': 289.46,\n",
       "    'feels_like': 285.51,\n",
       "    'temp_min': 289.46,\n",
       "    'temp_max': 289.46,\n",
       "    'pressure': 1009,\n",
       "    'sea_level': 1009,\n",
       "    'grnd_level': 1007,\n",
       "    'humidity': 75,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 804,\n",
       "     'main': 'Clouds',\n",
       "     'description': 'overcast clouds',\n",
       "     'icon': '04d'}],\n",
       "   'clouds': {'all': 100},\n",
       "   'wind': {'speed': 6.47, 'deg': 282},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0.97,\n",
       "   'sys': {'pod': 'd'},\n",
       "   'dt_txt': '2020-07-26 18:00:00'},\n",
       "  {'dt': 1595797200,\n",
       "   'main': {'temp': 285.53,\n",
       "    'feels_like': 283.79,\n",
       "    'temp_min': 285.53,\n",
       "    'temp_max': 285.53,\n",
       "    'pressure': 1010,\n",
       "    'sea_level': 1010,\n",
       "    'grnd_level': 1008,\n",
       "    'humidity': 88,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 804,\n",
       "     'main': 'Clouds',\n",
       "     'description': 'overcast clouds',\n",
       "     'icon': '04n'}],\n",
       "   'clouds': {'all': 98},\n",
       "   'wind': {'speed': 2.72, 'deg': 262},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0,\n",
       "   'sys': {'pod': 'n'},\n",
       "   'dt_txt': '2020-07-26 21:00:00'},\n",
       "  {'dt': 1595808000,\n",
       "   'main': {'temp': 283.69,\n",
       "    'feels_like': 281.55,\n",
       "    'temp_min': 283.69,\n",
       "    'temp_max': 283.69,\n",
       "    'pressure': 1011,\n",
       "    'sea_level': 1011,\n",
       "    'grnd_level': 1010,\n",
       "    'humidity': 90,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 804,\n",
       "     'main': 'Clouds',\n",
       "     'description': 'overcast clouds',\n",
       "     'icon': '04n'}],\n",
       "   'clouds': {'all': 85},\n",
       "   'wind': {'speed': 2.73, 'deg': 251},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0,\n",
       "   'sys': {'pod': 'n'},\n",
       "   'dt_txt': '2020-07-27 00:00:00'},\n",
       "  {'dt': 1595818800,\n",
       "   'main': {'temp': 283.67,\n",
       "    'feels_like': 281.84,\n",
       "    'temp_min': 283.67,\n",
       "    'temp_max': 283.67,\n",
       "    'pressure': 1011,\n",
       "    'sea_level': 1011,\n",
       "    'grnd_level': 1009,\n",
       "    'humidity': 90,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 804,\n",
       "     'main': 'Clouds',\n",
       "     'description': 'overcast clouds',\n",
       "     'icon': '04n'}],\n",
       "   'clouds': {'all': 93},\n",
       "   'wind': {'speed': 2.29, 'deg': 236},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0,\n",
       "   'sys': {'pod': 'n'},\n",
       "   'dt_txt': '2020-07-27 03:00:00'},\n",
       "  {'dt': 1595829600,\n",
       "   'main': {'temp': 285.58,\n",
       "    'feels_like': 284.49,\n",
       "    'temp_min': 285.58,\n",
       "    'temp_max': 285.58,\n",
       "    'pressure': 1011,\n",
       "    'sea_level': 1011,\n",
       "    'grnd_level': 1009,\n",
       "    'humidity': 85,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 804,\n",
       "     'main': 'Clouds',\n",
       "     'description': 'overcast clouds',\n",
       "     'icon': '04d'}],\n",
       "   'clouds': {'all': 92},\n",
       "   'wind': {'speed': 1.61, 'deg': 197},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0,\n",
       "   'sys': {'pod': 'd'},\n",
       "   'dt_txt': '2020-07-27 06:00:00'},\n",
       "  {'dt': 1595840400,\n",
       "   'main': {'temp': 288.24,\n",
       "    'feels_like': 287.07,\n",
       "    'temp_min': 288.24,\n",
       "    'temp_max': 288.24,\n",
       "    'pressure': 1011,\n",
       "    'sea_level': 1011,\n",
       "    'grnd_level': 1009,\n",
       "    'humidity': 74,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 804,\n",
       "     'main': 'Clouds',\n",
       "     'description': 'overcast clouds',\n",
       "     'icon': '04d'}],\n",
       "   'clouds': {'all': 100},\n",
       "   'wind': {'speed': 1.93, 'deg': 189},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0.19,\n",
       "   'sys': {'pod': 'd'},\n",
       "   'dt_txt': '2020-07-27 09:00:00'},\n",
       "  {'dt': 1595851200,\n",
       "   'main': {'temp': 286.57,\n",
       "    'feels_like': 284.69,\n",
       "    'temp_min': 286.57,\n",
       "    'temp_max': 286.57,\n",
       "    'pressure': 1010,\n",
       "    'sea_level': 1010,\n",
       "    'grnd_level': 1008,\n",
       "    'humidity': 89,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 500,\n",
       "     'main': 'Rain',\n",
       "     'description': 'light rain',\n",
       "     'icon': '10d'}],\n",
       "   'clouds': {'all': 100},\n",
       "   'wind': {'speed': 3.42, 'deg': 152},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0.99,\n",
       "   'rain': {'3h': 1.5},\n",
       "   'sys': {'pod': 'd'},\n",
       "   'dt_txt': '2020-07-27 12:00:00'},\n",
       "  {'dt': 1595862000,\n",
       "   'main': {'temp': 288.41,\n",
       "    'feels_like': 288.14,\n",
       "    'temp_min': 288.41,\n",
       "    'temp_max': 288.41,\n",
       "    'pressure': 1007,\n",
       "    'sea_level': 1007,\n",
       "    'grnd_level': 1005,\n",
       "    'humidity': 94,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 501,\n",
       "     'main': 'Rain',\n",
       "     'description': 'moderate rain',\n",
       "     'icon': '10d'}],\n",
       "   'clouds': {'all': 100},\n",
       "   'wind': {'speed': 2.34, 'deg': 163},\n",
       "   'visibility': 3752,\n",
       "   'pop': 1,\n",
       "   'rain': {'3h': 5.92},\n",
       "   'sys': {'pod': 'd'},\n",
       "   'dt_txt': '2020-07-27 15:00:00'},\n",
       "  {'dt': 1595872800,\n",
       "   'main': {'temp': 289.57,\n",
       "    'feels_like': 289.77,\n",
       "    'temp_min': 289.57,\n",
       "    'temp_max': 289.57,\n",
       "    'pressure': 1006,\n",
       "    'sea_level': 1006,\n",
       "    'grnd_level': 1004,\n",
       "    'humidity': 92,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 500,\n",
       "     'main': 'Rain',\n",
       "     'description': 'light rain',\n",
       "     'icon': '10d'}],\n",
       "   'clouds': {'all': 100},\n",
       "   'wind': {'speed': 2.08, 'deg': 314},\n",
       "   'visibility': 5555,\n",
       "   'pop': 1,\n",
       "   'rain': {'3h': 1.94},\n",
       "   'sys': {'pod': 'd'},\n",
       "   'dt_txt': '2020-07-27 18:00:00'},\n",
       "  {'dt': 1595883600,\n",
       "   'main': {'temp': 286.75,\n",
       "    'feels_like': 284.61,\n",
       "    'temp_min': 286.75,\n",
       "    'temp_max': 286.75,\n",
       "    'pressure': 1008,\n",
       "    'sea_level': 1008,\n",
       "    'grnd_level': 1006,\n",
       "    'humidity': 93,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 501,\n",
       "     'main': 'Rain',\n",
       "     'description': 'moderate rain',\n",
       "     'icon': '10n'}],\n",
       "   'clouds': {'all': 100},\n",
       "   'wind': {'speed': 4.16, 'deg': 306},\n",
       "   'visibility': 9994,\n",
       "   'pop': 0.92,\n",
       "   'rain': {'3h': 3.48},\n",
       "   'sys': {'pod': 'n'},\n",
       "   'dt_txt': '2020-07-27 21:00:00'},\n",
       "  {'dt': 1595894400,\n",
       "   'main': {'temp': 285.03,\n",
       "    'feels_like': 282.19,\n",
       "    'temp_min': 285.03,\n",
       "    'temp_max': 285.03,\n",
       "    'pressure': 1010,\n",
       "    'sea_level': 1010,\n",
       "    'grnd_level': 1008,\n",
       "    'humidity': 90,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 804,\n",
       "     'main': 'Clouds',\n",
       "     'description': 'overcast clouds',\n",
       "     'icon': '04n'}],\n",
       "   'clouds': {'all': 100},\n",
       "   'wind': {'speed': 4.23, 'deg': 294},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0.83,\n",
       "   'sys': {'pod': 'n'},\n",
       "   'dt_txt': '2020-07-28 00:00:00'},\n",
       "  {'dt': 1595905200,\n",
       "   'main': {'temp': 282.2,\n",
       "    'feels_like': 279.14,\n",
       "    'temp_min': 282.2,\n",
       "    'temp_max': 282.2,\n",
       "    'pressure': 1011,\n",
       "    'sea_level': 1011,\n",
       "    'grnd_level': 1009,\n",
       "    'humidity': 92,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 804,\n",
       "     'main': 'Clouds',\n",
       "     'description': 'overcast clouds',\n",
       "     'icon': '04n'}],\n",
       "   'clouds': {'all': 87},\n",
       "   'wind': {'speed': 3.65, 'deg': 270},\n",
       "   'visibility': 9549,\n",
       "   'pop': 0,\n",
       "   'sys': {'pod': 'n'},\n",
       "   'dt_txt': '2020-07-28 03:00:00'},\n",
       "  {'dt': 1595916000,\n",
       "   'main': {'temp': 282.49,\n",
       "    'feels_like': 279.03,\n",
       "    'temp_min': 282.49,\n",
       "    'temp_max': 282.49,\n",
       "    'pressure': 1012,\n",
       "    'sea_level': 1012,\n",
       "    'grnd_level': 1010,\n",
       "    'humidity': 92,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 803,\n",
       "     'main': 'Clouds',\n",
       "     'description': 'broken clouds',\n",
       "     'icon': '04d'}],\n",
       "   'clouds': {'all': 63},\n",
       "   'wind': {'speed': 4.31, 'deg': 268},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0,\n",
       "   'sys': {'pod': 'd'},\n",
       "   'dt_txt': '2020-07-28 06:00:00'},\n",
       "  {'dt': 1595926800,\n",
       "   'main': {'temp': 287.23,\n",
       "    'feels_like': 283.79,\n",
       "    'temp_min': 287.23,\n",
       "    'temp_max': 287.23,\n",
       "    'pressure': 1014,\n",
       "    'sea_level': 1014,\n",
       "    'grnd_level': 1012,\n",
       "    'humidity': 79,\n",
       "    'temp_kf': 0},\n",
       "   'weather': [{'id': 802,\n",
       "     'main': 'Clouds',\n",
       "     'description': 'scattered clouds',\n",
       "     'icon': '03d'}],\n",
       "   'clouds': {'all': 40},\n",
       "   'wind': {'speed': 5.17, 'deg': 289},\n",
       "   'visibility': 10000,\n",
       "   'pop': 0,\n",
       "   'sys': {'pod': 'd'},\n",
       "   'dt_txt': '2020-07-28 09:00:00'}],\n",
       " 'city': {'id': 2964574,\n",
       "  'name': 'Dublin',\n",
       "  'coord': {'lat': 53.344, 'lon': -6.2672},\n",
       "  'country': 'IE',\n",
       "  'population': 15000,\n",
       "  'timezone': 3600,\n",
       "  'sunrise': 1595478456,\n",
       "  'sunset': 1595536511}}"
      ]
     },
     "execution_count": 250,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "forecast"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 258,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1595505600\n",
      "15.75\n",
      "Clouds\n"
     ]
    }
   ],
   "source": [
    "print(forecast['list'][0]['dt'])\n",
    "print(forecast['list'][0]['main']['feels_like']-273.15)\n",
    "print(forecast['list'][0]['weather'][0]['main'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 271,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'feels_like': 16.230000000000018, 'weather_main': 'Clouds'}"
      ]
     },
     "execution_count": 271,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "find_forecast(today)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 272,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2020, 7, 23, 11, 16, 33, 536202)"
      ]
     },
     "execution_count": 272,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "today"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 273,
   "metadata": {},
   "outputs": [],
   "source": [
    "other_day = datetime.datetime(2020,7,28,11,16,33,536202)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Mesel\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:31: UserWarning: Boolean Series key will be reindexed to match DataFrame index.\n",
      "C:\\Users\\Mesel\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:32: UserWarning: Boolean Series key will be reindexed to match DataFrame index.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "347.3990886957432"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_prediction('1',1,'2020-07-08 18:20:20',48,44)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 303,
   "metadata": {},
   "outputs": [],
   "source": [
    "datestring = \"2020-7-4 10:10:10\"\n",
    "datetime_departure = datetime.datetime.strptime(datestring,'%Y-%m-%d %H:%M:%S')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 299,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2020, 7, 4, 10, 10, 10)"
      ]
     },
     "execution_count": 299,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime_departure"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 297,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
